<?php
/*
 * 此文件用于验证短信服务API接口，供开发时参考
 * 执行验证前请确保文件为utf-8编码，并替换相应参数为您自己的信息，并取消相关调用的注释
 * 建议验证前先执行Test.php验证PHP环境
 *
 * 2017/11/30
 */
namespace sms;
class SendSms{
//    private $accessKeyId = "LTAIQI3gFlSTpGqE";
//    private $accessKeySecret = "1HhvIKg1Tecx223MPwQ5HlwhP8IeHn";
//    private $signName = "河长E巡";
//    private $templateCode = "SMS_208335291";


//    private $accessKeyId = "LTAI4GFXVDVVEb2LcEh8ZqA9";
//    private $accessKeySecret = "6ru3ZIlj7ytNhGTX8tXgq8hZdvXWG4";
//    private $signName = "身份验证";
//    private $templateCode = "SMS_191630228";


//    private $accessKeyId = "LTAIQI3gFlSTpGqE";
//    private $accessKeySecret = "1HhvIKg1Tecx223MPwQ5HlwhP8IeHn";
//    private $signName = "全域治水";
//    private $templateCode = "SMS_193237269";

    private $accessKeyId = "LTAIQI3gFlSTpGqE";
    private $accessKeySecret = "1HhvIKg1Tecx223MPwQ5HlwhP8IeHn";
    private $signName = "山清水秀";
    private $templateCode = "SMS_193237269";

    //发送短信验证码
    public function sendSmsCode($phone,$code){

        if(!$phone || !$code) return false;

        $params = array ();
        // fixme 必填：是否启用https
        $security = false;

        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = $this->signName;

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $phone;
        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = $this->templateCode;

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array (
            "code" => $code,
        );

        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"]);
        }


        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求

        $helper = new \sms\SignatureHelper();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $this->accessKeyId,
            $this->accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            )),
            $security
        );


        return $content;
    }
    /**
     * 短信发送记录查询
     */
   public function querySendDetails($phone) {
        if(!$phone) return false;
        $params = array ();

        // *** 需用户填写部分 ***
        // fixme 必填：是否启用https
        $security = false;



        // fixme 必填: 短信接收号码
        $params["PhoneNumber"] = $phone;

        // fixme 必填: 短信发送日期，格式Ymd，支持近30天记录查询
        $params["SendDate"] = date('Ymd');

        // fixme 必填: 分页大小
        $params["PageSize"] = 10;

        // fixme 必填: 当前页码
        $params["CurrentPage"] = 1;

        // fixme 可选: 设置发送短信流水号
        $params["BizId"] = "yourBizId";

        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new SignatureHelper();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $this->accessKeyId,
            $this->accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "QuerySendDetails",
                "Version" => "2017-05-25",
            )),
            $security
        );

        return $content;
    }


}

